
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.automationagent;

import java.rmi.Naming;
import javax.rmi.PortableRemoteObject;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.URL;

/**
 * AAClient
 * @author Ugorji
 */
public class AAClient {
  //String is in the form //<hostname>:<port>/<bindname> e.g. //localhost:9001/AAHandler
  private String urlstring;
  
  /**
   * Need to explicitly pass a URL
   * The URL passed is of the form //<hostname>:<port>/<bindname>
   * e.g. //prospect:8001/ABC means
   * AAHandler is bound to registry running on prospect at port 8001, under the bind name ABC
   */
  public AAClient(String url) {  
    urlstring = url;
  }
  
  /**
   * Retrieve the AAHandler bound to the passed URL
   */
  public AAHandler getHandler() throws Exception {
    URL url = new URL("http:" + urlstring);
    RMISocketFactory rmisf = RMISocketFactory.getDefaultSocketFactory();
    Registry registry = LocateRegistry.getRegistry(url.getHost(), url.getPort(), rmisf);
    String lookupname = url.getPath().substring(1);
    Object obj = registry.lookup(lookupname);
    //Object obj = Naming.lookup(urlstring);
    // System.out.println(String.valueOf(obj));
    AAHandler clh = (AAHandler)PortableRemoteObject.narrow(obj, AAHandler.class);
    return clh;

  }  
  
  /**
   * Command line to invoke an action on the AAHandler
   * The first argument is the action, all others constitute the args to the action
   */
  public static void main(String[] args) throws Exception {
    String rmiurl = args[0];
    String action = args[1];
    String[] args2 = new String[args.length - 2];
    System.arraycopy(args, 2, args2, 0, args2.length);
    
    AAClient aaclient = new AAClient(rmiurl);
    aaclient.getHandler().invoke(action, args);
  } 
  
   
}
